package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Model.Anwesenheit;
import Model.Schueler;

public class SchuelerDAO {

	
private KlasseDAO klasseDAO=new KlasseDAO();
	
	public Schueler getSchueler(int id)
	{
		Schueler schueler=null;
		Connection conn=ConnectionProvider.getConnection();
		PreparedStatement statement=null;
		ResultSet rs=null;
		String sql="select * from schueler where schuelerid=?";
        try {
			statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			rs=statement.executeQuery();
			
			if (rs.next())
			{
				schueler=new Schueler();
				schueler.setId(id);
				schueler.setSvorname(rs.getString("svorname"));
				schueler.setSname(rs.getString("sname"));
				schueler.setKlasse(klasseDAO.getKlasse(rs.getInt("klasseID")));
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				rs.close();
				statement.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return schueler;
	}
	
	public void update(Schueler schueler)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="update schueler set svorname=?, sname=?, klasseID=? where schuelerID=?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setString(1, schueler.getSvorname());
			statement.setString(2, schueler.getSname());
			statement.setInt(3, schueler.getKlasse().getId());
			statement.setInt(4, schueler.getId());

			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void anlegen(Schueler schueler)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="insert into schueler VALUES (?, ?, ?, ?)";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, schueler.getId());
			statement.setString(2, schueler.getSvorname());
			statement.setString(3, schueler.getSname());
			statement.setInt(4, schueler.getKlasse().getId());
			
			statement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void delete(int id)
	{
		Connection conn=ConnectionProvider.getConnection();
		String sql="delete from schueler where schuelerID= ?";
        try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, id);
			statement.executeUpdate();
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
        finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
